package com.bibrApp.difu.actvity.pay_setting_mvp;

import android.content.Context;
import android.text.TextUtils;

import com.bibrApp.difu.Response.PaySettingResPonseBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class PaySettingPresenter extends BasePresenter<IPaySettingView> {
    public PaySettingPresenter(Context context, IPaySettingView iView) {
        super(context, iView);
    }

    /**
     * 新增用户支付方式
     * name	String	是	备注
     * userId	String	是	userId
     * memberId	String	是	memberId
     * walletAddress	String	是	钱包地址
     * payPwd	String	是	资金安全密码
     * coinEunit	String	是	USDT、BTC、ETH
     */
    public void addPatSetting(String name, String userId,String memberId,String walletAddress ,String payPwd,String coinEunit) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("userId", userId);
        map.put("memberId", memberId);
        map.put("walletAddress", walletAddress);
        map.put("payPwd", payPwd);
        map.put("coinEunit", coinEunit);
        RetrofitNet.getInstance()
                .getApi()
                .addPaySetting(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            ToastUtils.ShowToast(context, String.valueOf(value));
                        }

                    }
                });
    }

    public void getAllPaySetting() {
        RetrofitNet.getInstance()
                .getApi()
                .queryAllPaySetting()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context,false) {

                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            String jsonStr = EncryptUtils.objectToJson(value);
                            if(TextUtils.isEmpty(jsonStr)){
                                return;
                            }
                            List<PaySettingResPonseBody> datas = new Gson().fromJson(jsonStr,new TypeToken<List<PaySettingResPonseBody>>(){}.getType());
                            iView.successOfAllPaySetting(datas);
                        }

                    }
                });
    }
}
